package com.gitee.wsl.compose.chart.pie.model

import com.gitee.wsl.unit.angle.AngleUnit
import com.gitee.wsl.unit.angle.deg

/**
 * Defines the angular position of a single Pie Slice in terms of the starting angle for the slice, [startAngle], and
 * the angular size of the slice, [angle]. 0-degrees is to the right and angles increase in the clockwise
 * direction.
 */
data class PieSliceData(
    val startAngle: AngleUnit,
    val angle: AngleUnit,
)

fun makePieSliceData(
    data: List<Float>,
    beta: Float,
    pieStartAngle: AngleUnit,
    pieExtendAngle: AngleUnit,
): List<PieSliceData> {
    val total = data.sumOf {
        it.toDouble()
    }.toFloat().let {
        if (it == 0F) {
            1F
        } else {
            it
        }
    }

    return buildList {
        var startAngle = pieStartAngle.degrees
        val pieExtend = pieExtendAngle.degrees
        for (i in data.indices) {
            val extent = data[i] / total * pieExtend * beta
            add(PieSliceData(startAngle.deg, extent.deg))
            startAngle += extent
        }
    }
}